package servlet;

import bean.NewsofUser;
import dao.NewsDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/newsview.do")
public class NewsView extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public NewsView() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        request.setCharacterEncoding("UTF-8");
//        response.setCharacterEncoding("UTF-8");

        String id = request.getParameter("newsid");
        System.out.println(id);
            int newsid = Integer.parseInt(id);
            Object[] news = NewsDao.select(newsid);
        if (news != null) {
            response.getWriter().print("<!DOCTYPE html>\n" +
                    "<html lang=\"en\">\n" +
                    "<head>\n" +
                    "    <meta charset=\"UTF-8\">\n" +
                    "    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n" +
                    "    <title>News Page</title>\n" +
                    "    <style>\n" +
                    "        body {\n" +
                    "            font-family: Arial, sans-serif;\n" +
                    "            margin: 20px;\n" +
                    "            background-color: #f5f5f5;\n" +
                    "        }\n" +
                    "        .news-container {\n" +
                    "            max-width: 800px;\n" +
                    "            margin: 0 auto;\n" +
                    "            padding: 20px;\n" +
                    "            background-color: #fff;\n" +
                    "            border-radius: 8px;\n" +
                    "            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);\n" +
                    "        }\n" +
                    "        .news-container img {\n" +
                    "            max-width: 100%;\n" +
                    "            height: auto;\n" +
                    "            border-radius: 8px;\n" +
                    "            margin-bottom: 20px;\n" +
                    "        }\n" +
                    "        .news-container p {\n" +
                    "            line-height: 1.6;\n" +
                    "            font-size: 18px;\n" +
                    "            color: #333;\n" +
                    "        }\n" +
                    "        .news-title {\n" +
                    "            font-size: 32px; /* 加大字号 */\n" +
                    "            font-weight: bold;\n" +
                    "            margin-bottom: 10px;\n" +
                    "            color: #333;\n" +
                    "            text-align: center; /* 居中对齐 */\n" +
                    "        }\n" +
                    "        .news-meta {\n" +
                    "            text-align: center;\n" +
                    "            color: #777;\n" +
                    "            margin-bottom: 20px;\n" +
                    "        }\n" +
                    "    </style>\n" +
                    "</head>\n" +
                    "<body>\n" +
                    "    <div class=\"news-container\">\n" +
                    "        <div class=\"news-title\">" + news[0] + "</div>\n" +
                    "        <div class=\"news-meta\">\n" +
                    "            <span>发布人: " + news[2] + "</span>\n" +
                    "            <span> |    " + news[3] + "</span>\n" +
                    "        </div>\n" +
                    "        " + news[1] + "\n" +"<br/><br/><br/><br/>"+
                    "<span class=\"news-meta\">  CQIE新闻，未经授权不得转载 </span>\n"+

                    "    </div>\n" +

                    "</body>\n" +
                    "</html>");

        }
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
